import axios from "axios";
import WebUtils from "@/utils/webUtils";
import router1 from "@/router";
const apiUrl = "http://localhost:8081";

export const userLogin = async (loginData) => {
    try {//用户登录接口
        const response = await axios.post(`${apiUrl}/login`, loginData);
        return response.data;
    } catch (error) {

        throw error;
    }
};

export const sendCode = async (userPhone) => {
    try {//发送验证码接口
        const response = await axios.get(`${apiUrl}/code`, {
            params: {userPhone},
        })
        return response.data;
    } catch (error) {

        throw error;
    }
};

export const userRegister = async (registerData) => {
    try {//用户注册接口
        const response = await axios.post(`${apiUrl}/register`, registerData);
        return response.data;
    } catch (error) {

        throw error;
    }
};

export const userCharge = async (money) => {
    try {//用户充值接口
        WebUtils.addToken();
        const response = await
            axios.put(`${apiUrl}/user/charge?money=` + money)
        WebUtils.isRedirect(response.data.code);
        return response.data;
    } catch (error) {

        throw error;
    }
};

export const getUserInfo = async () => {
    try {//获取用户信息接口
        WebUtils.addToken();
        const response = await
            axios.get(`${apiUrl}/user/current`);
        WebUtils.isRedirect(response.data.code);
        return response.data;
    } catch (error) {
        throw error;
    }
};

export const changeUser = async (form) => {
    WebUtils.addToken();
    try {//修改用户信息接口
        const response = await
            axios.post(`${apiUrl}/user/change`, form, {
                headers: {
                    'Content-Type': 'application/json',
                },
            });
        WebUtils.isRedirect(response.data.code);
        return response.data;
    } catch (error) {
        throw error;
    }
};

export const setProductCart = async (productCart) => {
    try {
        WebUtils.addToken();
        const response = await axios.post(`${apiUrl}/cart`, productCart, {
            headers: {
                "Content-Type": "application/json",
            }
        });
        WebUtils.isRedirect(response.data.code);
        return response.data;
    } catch (error) {
        throw error;
    }
}

export const getCartPrice = async () => {
    try {//获取购物车中产品总价格
        WebUtils.addToken();
        const response = await axios.get(`${apiUrl}/cart/price`, {
            headers: {
                "token": localStorage.getItem("token"),
            },
        });
        WebUtils.isRedirect(response.data.code);
        return response.data;
    } catch (error) {
        console.log(error)
        throw error;
    }
}

export const payOrder = async (productcart) => {
    try {//支付订单
        WebUtils.addToken();
        const response = await axios.put(`${apiUrl}/order/pay`, productcart);
        WebUtils.isRedirect(response.data.code);
        return response.data;
    } catch (error) {
        throw error;
    }
}

export const getCartList = async () => {
    try {//获取购物车中的产品
        WebUtils.addToken();
        const response = await axios.get(`${apiUrl}/cart`);
        WebUtils.isRedirect(response.data.code);
        return response.data;
    } catch (error) {
        throw error;
    }
}

export const updateCartProduct = async (updateCart) => {
    WebUtils.addToken();
    try {//更新购物车中的产品数量
        const response = await axios.put(`${apiUrl}/cart`, updateCart);
        WebUtils.isRedirect(response.data.code);
        return response.data;

    } catch (error) {
        throw error;
    }
}

export const delCartProduct = async (productId) => {
    try {//删除购物车中的产品
        WebUtils.addToken();
        const response = await axios.delete(`${apiUrl}/cart`, {
            params: {
                productId: productId,
            }
        });
        WebUtils.isRedirect(response.data.code);
        return response.data;
    } catch (error) {
        throw error;
    }
}

export const getUserOrder = async (userId) => {
    try {//获取用户自己的订单
        WebUtils.addToken();

        const response = await
            axios.get(`${apiUrl}/order/me?userId=` + userId);
        WebUtils.isRedirect(response.data.code);
        return response.data;
    } catch (error) {
        throw error;
    }
}

export const submitOrder = async () => {
    try {//提交订单
        WebUtils.addToken();
        const response = await axios.post(`${apiUrl}/submit`);
        WebUtils.isRedirect(response.data.code);
        return response.data;
    } catch (error) {
        throw error;
    }
}

export const currentUser = async () => {
    try {
        WebUtils.addToken();
        const response = await axios.get(`${apiUrl}/user/current`);
        WebUtils.isRedirect(response.data.code);
        return response.data;
    } catch (error) {
        throw error;
    }

}
export const logout = async () => {
    try {
        WebUtils.addToken();
        const response = await axios.get(`${apiUrl}/user/logout`);
        localStorage.removeItem("token");
        WebUtils.isRedirect();
        return response.data;
    } catch (error) {
        throw error;
    }
}

